import EventItem from "./EventItem";
import EventName from "./EventName";


const { ccclass, property } = cc._decorator;

@ccclass
export default class EventContent extends cc.Component {

    @property(cc.ScrollView)
    scrollview: cc.ScrollView = null;

    @property(cc.Prefab)
    eventItem: cc.Prefab = null;

    start() {
        cc.director.getScene().on(EventName.UPDATE_GAME_CONTENT, this.onAddAgeEvent, this);
    }

    private onAddAgeEvent() {
        let item = cc.instantiate(this.eventItem);
        item.setParent(this.scrollview.content);
        item.getComponent(EventItem).init(this.scrollview.content.children.length - 1);
        this.checkUI();
    }

    private checkUI() {
        let len = this.scrollview.content.children.length;
        let lastItem = this.scrollview.content.children[len - 1];

        //检查坐标，如果不在视野里面，则显示往下移动todo
        if (len > 6) {
            let moveLen = lastItem.height + this.scrollview.content.getComponent(cc.Layout).spacingY;
            let moveTime = moveLen / 500;
            cc.tween(this.scrollview.content)
                .by(moveTime, { "y": moveLen })
                .start();
        }
    }

}
